package com.yx.cloud.product.laser.yxcloud;

import android.graphics.Color;

import com.iot.common.utils.Event;
import com.iot.product.sweeper.bean.BaseStation;
import com.iot.sweeper.param.BaseStationSettingParam;
import com.yx.cloud.R;

import org.json.JSONException;
import org.json.JSONObject;

public class X5_MAX extends X3_YXCLOUD {
    public static final String PID = "x5-max";
    public static final String DP_LIGHT = "136";
    public static final String DP_AIRCLEANER_SWITCH = "140";
    public static final String DP_AIR_PM25 = "141";
    public static final String DP_AIRCLEANER = "142";
    public static final String DP_AIRCLEANER_STANDARD_LIFE = "143";
    public static final String DP_AIRCLEANER_RESET_STANDARD_LIFE = "144";
    public static final String DP_AIRCLEANER_HCHO_LIFE = "145";
    public static final String DP_AIRCLEANER_RESET_HCHO_LIFE = "146";
    public static final String DP_AIRCLEANER_FILTER_TYPE = "147";
    public static final String DP_AIR_QUALITY = "148";
    public static final String DP_AIRCLEANER_FILTER_FAULT_TYPE = "149";
    public static final String DP_BASE_STATION_ISOFFLINE = "151";

    private X5_MAX() {
        setProductId(PID);
        getProduct().setProductName("X5");
        getProduct().setProductIcon(R.mipmap.robot_icon_pic_x5);
    }


    @Override
    public void initConfig() {
        super.initConfig();
        X5MaxConfig.initConfig(getProduct().getConfigureMap(), this);
    }

    @Override
    public void parseDp(JSONObject dataJson) throws JSONException {
        super.parseDp(dataJson);

        JSONObject jsonObject = dataJson.getJSONObject("thing_status");
        if (jsonObject.has(DP_LIGHT)) {
            //灯光开关
            String lightStr = jsonObject.getString(DP_LIGHT);
            parseLightCmd(baseStation, lightStr);
            Event.newBuilder().type(Event.MEG_ROBOT_BASESTATION_LIGHT).content(baseStation).result(true).send();
        }

        if (jsonObject.has(DP_AIRCLEANER_SWITCH)) {
            //空气净化器开关
            int airCleanState = jsonObject.getInt(DP_AIRCLEANER_SWITCH);
            if (0 == airCleanState) {
                baseStation.setAirCleanerSwitch(false);
                baseStation.setPlasmaSwitch(false);
            } else if (1 == airCleanState) {
                baseStation.setAirCleanerSwitch(true);
                baseStation.setPlasmaSwitch(true);
            } else if (2 == airCleanState) {
                baseStation.setAirCleanerSwitch(true);
                baseStation.setPlasmaSwitch(false);
            }
            Event.newBuilder().type(Event.MEG_ROBOT_BASESTATION_AIRCLEAN).content(baseStation).result(true).send();
        }

        if (jsonObject.has(DP_AIRCLEANER)) {
            //空气净化器档位
            int airStation = jsonObject.getInt(DP_AIRCLEANER);
            baseStation.setAirCleaner(airStation);
            Event.newBuilder().type(Event.MEG_ROBOT_BASESTATION_AIRCLEAN).content(baseStation).result(true).send();
        }

        if (jsonObject.has(DP_AIRCLEANER_STANDARD_LIFE)) {
            //空气净化器滤芯剩余寿命
            int life = jsonObject.getInt(DP_AIRCLEANER_STANDARD_LIFE);
            baseStation.setAirCleanerLife(life);
            Event.newBuilder().type(Event.MEG_ROBOT_BASESTATION_AIRCLEAN).content(baseStation).result(true).send();
        }
        if (jsonObject.has(DP_AIRCLEANER_HCHO_LIFE)) {
            //空气净化器滤芯剩余寿命
            int life = jsonObject.getInt(DP_AIRCLEANER_HCHO_LIFE);
            baseStation.setHchoAirCleanerLife(life);
            Event.newBuilder().type(Event.MEG_ROBOT_BASESTATION_AIRCLEAN).content(baseStation).result(true).send();
        }


        if (jsonObject.has(DP_AIRCLEANER_FILTER_TYPE)) {
            //空气净化器滤芯类型
            String type = jsonObject.getString(DP_AIRCLEANER_FILTER_TYPE);
            if ("hcho".equals(type)) {
                //hcho
                baseStation.setAirFilterType(BaseStation.AIR_FILTER_HCHO);
            } else {
                //standard
                baseStation.setAirFilterType(BaseStation.AIR_FILTER_STANDARD);
            }
            Event.newBuilder().type(Event.MEG_ROBOT_BASESTATION_AIRCLEAN).content(baseStation).result(true).send();
        }


        if (jsonObject.has(DP_AIR_PM25)) {
            //PM2.5  0-999
            int PM25 = jsonObject.getInt(DP_AIR_PM25);
            baseStation.setPm25(PM25);
            Event.newBuilder().type(Event.MEG_ROBOT_BASESTATION_AIRCLEAN).content(baseStation).result(true).send();
        }

        if (jsonObject.has(DP_AIR_QUALITY)) {
            //空气质量
            String qualityStr = jsonObject.getString(DP_AIR_QUALITY);
            int quality = 0;
            if ("good".equals(qualityStr)) {
                quality = 0;
            } else if ("medium".equals(qualityStr)) {
                quality = 1;
            } else if ("severe".equals(qualityStr)) {
                quality = 2;
            }
            baseStation.setAirQuality(quality);
            Event.newBuilder().type(Event.MEG_ROBOT_BASESTATION_AIRCLEAN).content(baseStation).result(true).send();
        }
        if (jsonObject.has(DP_BASE_STATION_ISOFFLINE)) {
            //基站是否在线
            boolean isOffline = jsonObject.getBoolean(DP_BASE_STATION_ISOFFLINE);
            baseStation.setBaseStationIsOnLine(!isOffline);
            Event.newBuilder().type(Event.MEG_ROBOT_BASESTATION_STATE).content(baseStation).result(true).send();
        }
    }

    @Override
    public void setBaseStationSetting(BaseStationSettingParam param) {
        if (param.isHasset_light()) {
            String cmd = getLightCmd(param);
            sendToDevice(DP_LIGHT, cmd);
        }

        if (param.isHasset_airCleanerSwitch() && param.isHasset_plasmaSwitch()) {
            if (param.getAirCleanerSwitch()) {
                if (param.isPlasmaSwitch()) {
                    sendToDevice(DP_AIRCLEANER_SWITCH, 1);
                } else {
                    sendToDevice(DP_AIRCLEANER_SWITCH, 2);
                }
            } else {
                sendToDevice(DP_AIRCLEANER_SWITCH, 0);
            }
        }

        if (param.isHasset_airCleaner()) {
            sendToDevice(DP_AIRCLEANER, param.getAirCleaner());
        }

        if (param.isHasset_resetFilter()) {
            if (param.getResetFilterType() == BaseStation.AIR_FILTER_HCHO) {
                sendToDevice(DP_AIRCLEANER_RESET_HCHO_LIFE, true);
            } else {
                sendToDevice(DP_AIRCLEANER_RESET_STANDARD_LIFE, true);
            }
        }
    }

    public void parseLightCmd(BaseStation baseStation, String cmd) {
        if (cmd.length() == 10) {
            try {
                int switcher = Integer.parseInt(cmd.substring(0, 2), 16);
//                int color = Integer.parseInt("FF" + cmd.substring(2, 8),16);
                int color = Color.parseColor("#ff" + cmd.substring(2, 8));
                int brightness = Integer.parseInt(cmd.substring(8), 16);
                baseStation.setLightSwitch(switcher != 0);
                if (switcher == 2) {
                    baseStation.setLightType(BaseStation.LIGHT_TYPE_COLORFUL);
                } else if (switcher == 3) {
                    baseStation.setLightType(BaseStation.LIGHT_TYPE_RAINBOW);
                } else {
                    baseStation.setLightType(BaseStation.LIGHT_TYPE_WHITE);
                }
                baseStation.setLightColor(color);
                baseStation.setBrightness(brightness);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    }

    private String getLightCmd(BaseStationSettingParam param) {

        int switcher;
        if (param.getLightSwitch()) {
            if (param.getLightType() == BaseStation.LIGHT_TYPE_COLORFUL) {
                switcher = 2;
            } else if (param.getLightType() == BaseStation.LIGHT_TYPE_RAINBOW) {
                switcher = 3;
            } else {
                switcher = 1;
            }
        } else {
            switcher = 0;
        }

        int rgb = param.getLightColor();
        int r = (rgb & 0x00ff0000) >> 16;
        int g = (rgb & 0x0000ff00) >> 8;
        int b = rgb & 0x000000ff;

        String cmd = getHex(switcher) + getHex(r) + getHex(g) + getHex(b) + getHex(param.getBrightness());
        return cmd;
    }

    private String getHex(int i) {
        return i < 16 ? "0" + Integer.toHexString(i) : Integer.toHexString(i);
    }
}
